﻿hpxAdminApp.factory('billService', ['Restangular', function (Restangular) {
    var res = Restangular.all('bills');
    return {
        get: function (id) {
            return res.all('bill').one(id).get();
        },
        getAllBillUnchecked: function (params, filter) {
            var queryParam = {
                'checkedType': filter.checkedType,
                'contactPerson':filter.contactPerson,
                'contactPhone':filter.contactPhone,
                'billTypeId':filter.billTypeId,
                'tradeTypeCode':filter.tradeTypeCode,
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy()
            }
            return res.get('billCheck', queryParam).then(function (result) {
                params.total(result.page_info.items_number);
                return result.bills;
            });
        },
        newGetAllBillUnchecked: function (params, filter) {
            var queryParam = {
                'checkedType': filter.checkedType,
                'acceptorTypeId': filter.acceptorTypeId,
                'billNumber': filter.billNumber,
                'billTypeId': filter.billTypeId,
                'tradeTypeCode': filter.tradeTypeCode,
                'deadlineTimeStart': filter.deadlineTimeStart,
                'deadlineTimeEnd': filter.deadlineTimeEnd,
                'sumPriceMin': filter.sumPriceMin,
                'sumPriceMax': filter.sumPriceMax,
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy()
            }
            return res.get('billCheck', queryParam).then(function (result) {
                params.total(result.page_info.items_number);
                return result.bills;
            });
        },
        getAllBillProductsUnchecked: function (params, filter) {
            var queryParam = {
                'checkedType': filter.checkedType,
                'bill_status': filter.bill_status,
                'bill_number': filter.bill_number,
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy()
            }  //  + '/user' + '/billProduct'
            return res.get('billProduct' + '/user' + '/billProduct', queryParam).then(function (result) {
                if (result) {
                    params.total(result.page_info.items_number);
                    return result.listName;
                } else {
                    return ''
                }        
            });
        },
        getSingleBill: function (billId) {
            return res.all('billCheck').one(billId.toString()).get();
        },
        //getAllBillUnchecked: function () {
        //    return res.get('billCheck', {});
        //},
        getBillUnchecked: function (billId) {
            return res.all('billCheck').one(billId.toString()).get();
        },
        checkBillProduct: function (model) {
            return res.all('billCheck').post(model);
        },
        checkBill: function (model) {
            return res.all('billCheck').post(model);
        },
        updateBill: function (billProductId, model) {
            return res.all('bill').one(billProductId.toString() + '?adminUpdate=' + 1).customPUT(model);
        },
        updateBill2: function (billProductId, model) {
            console.log(billProductId)
            console.log(model)
            return res.all('bill').all('user').one(billProductId.toString()).customPUT(model);
        },
        removeBill: function (billProductId) {
            return res.all('billProduct').all('user').one(billProductId.toString()).customPUT();
        },
        billHoldingCenterList: function (params, filter) {
            var queryParam = {
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy(),
                'bill_type_id': filter.billType,
                'bill_sum_price_max': filter.bill_sum_price_max,
                'bill_sum_price_min': filter.bill_sum_price_min,
                'deadline_time_max': filter.deadline_time_max,
                'deadline_time_min': filter.deadline_time_min,
                'acceptor_type_id': filter.acceptType,
                'bill_rate_min': filter.bill_rate_min,
                'bill_rate_max': filter.bill_rate_max,
                'bill_status': filter.bill_status,
                'enterprise_name': filter.enterprise_name
            }
            return res.one('bill', 'stock').get(queryParam).then(function (result) {
                if (result) {
                    params.total(result.page_info.items_number);
                    return result.listName;
                }
            })
        },
        //票据商城列表
        getBillAllProduct: function (params, filters) {
            //var str_accept_type_id
            //if (filters.accept_type_ids.length == undefined) {
            //    str_accept_type_id = ''
            //} else {
            //    str_accept_type_id = filters.accept_type_ids.join(',');
            //}
            var queryParam = {
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy(),
                'bidding_mode': filters.bidding_mode,
                'trade_appoint_day': filters.trade_appoint_day,//票据产品交易期限
                'is_bidding': filters.is_bidding,
                'bill_status': filters.bill_status,
                'acceptor_type_id': filters.accept_type_ids.join(','),
                'bill_type_id': filters.billType,
                'bill_sum_price_min': filters.bill_sum_price_min,
                'bill_sum_price_max': filters.bill_sum_price_max,
                'bill_sum_price_type': filters.bill_sum_price_type,
                'deadline_time_min': filters.deadline_time_min,
                'deadline_time_max': filters.deadline_time_max,
                'product_location_id': filters.product_location_id,
                'offer_rate': filters.offer_rate,
                'offer_every_plus': filters.offer_every_plus,
                'bill_flaws': filters.bill_flaws,
                'acceptor_name': filters.acceptor_name
            }
            return res.one('billProduct', 'list').get(queryParam).then(function (result) {
                if (result == null || result == 'Close' || result == 'NotBeginning' || result == 'NonWorkday') {
                    return result;
                } else {
                    params.total(result.page_info.items_number);
                    result.listName.forEach(function (i) {
                        i.active = false
                    })
                    return result.listName;
                }

            });
        },
        getBillAllProductes: function (params, filters, p1) {
            var queryParam = {
                'p': p1,
                'n': params.count(),
                'orderBy': params.orderBy(),
                'bidding_mode': filters.bidding_mode,
                'trade_appoint_day': filters.trade_appoint_day,//票据产品交易期限
                'is_bidding': filters.is_bidding,
                'bill_status': filters.bill_status,
                'acceptor_type_id': filters.accept_type_ids.join(','),
                'bill_type_id': filters.billType,
                'bill_sum_price_min': filters.bill_sum_price_min,
                'bill_sum_price_max': filters.bill_sum_price_max,
                'bill_sum_price_type': filters.bill_sum_price_type,
                'deadline_time_min': filters.deadline_time_min,
                'deadline_time_max': filters.deadline_time_max,
                'product_location_id': filters.product_location_id,
                'offer_rate': filters.offer_rate,
                'offer_every_plus': filters.offer_every_plus,
                'bill_flaws': filters.bill_flaws,
                'acceptor_name': filters.acceptor_name
            }
            return res.one('billProduct', 'list').get(queryParam).then(function (result) {
                if (result == null || result == 'Close' || result == 'NotBeginning') {
                    return result;
                } else {
                    params.total(result.page_info.items_number);
                    result.listName.forEach(function (i) {
                        i.active = false
                    })
                    return result.listName;
                }

            });
        },
    }
}]);
